// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

//导入路由的包
/* import VueRouter from 'vue-router'
Vue.use(VueRouter) */

//注册 vuex
import Vuex from 'vuex'
Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    addToCar(state, goodsinfo){
      //假设在购物车中没有找到对应的商品
      var flag = false
      state.car.some(item => {
        if(item.id == goodsinfo.id) {
          item.count += parseInt(goodsinfo.count)
          flag = true
          return true
        }
      })
      //如果最终，循环完毕，得到的flag还是false，则把商品数据直接push到购物车中
      if(!flag) {
        state.car.push(goodsinfo)
      }
      //当更新car之后，把car数组存储到本地的localStorage中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsInfo(state, goodsinfo) {
      //修改购物车中商品的数量值
      // console.log(goodsinfo);
      state.car.some(item => {
        if(item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count)
          return true
        }
      })
      //当修改完商品的数量，把最新的购物车数据保存到本地存储中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    removeFormCar(state, id) {
      //根据Id从store中的购物车中删除对应的那条商品数据
      state.car.some((item, i) => {
        if(item.id == id) {
          state.car.splice(i, 1)
          return true;
        }
      })
      //将删除完毕后的最新的购物车数据同步到本地存储中
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsSelected(state, info) {
      //更新购物车中选择的商品数据
      state.car.some(item => {
        if(item.id == info.id) {
          item.selected = info.selected
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {
    //计算属性
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.count
      })
      return c
    },
    getGoodsCount(state) {
      //商品数
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.count
      })
      return o
    },
    getGoodsSelected(state) {
      //勾选数
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.selected
      })
      return o
    },
    getGoodsCountAndAmount(state) {
      //计算总价
      var o = {
        count: 0,   //勾选数量
        amount: 0   //勾选的总价
      }
      state.car.forEach(item => {
        if(item.selected) {
          o.count += item.count
          o.amount += item.price *  item.count
        }
      })
      return o
    }
  }
})

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
//设置请求的根路径
Vue.http.options.root = "http://027xin.com:8899/"
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;


//导入格式化时间的插件
import moment from 'moment'
//定义时间格式化全局过滤器
Vue.filter('dateFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss"){
  return moment(dataStr).format(pattern)
})

//导入mint-ui
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

Vue.config.productionTip = false

// 导入 MUI 的样式
import './lib/mui/css/mui.min.css'
// 导入扩展图标样式
import './lib/mui/css/icons-extra.css'
//导入全局样式表(缩略图)
import './lib/css/golbal.css'

import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入路由模块
import router from './router.js'

//导入 App根组件
import app from './App.vue'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
  /* components: { App }, */
  /* template: '<App/>' */
})
